package com.ctshk.rpc.order.ship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.ship.dto.OperationLogDTO;
import com.ctshk.rpc.order.ship.entity.OperationLog;
import com.ctshk.rpc.order.ship.mapper.OperationLogMapper;
import com.ctshk.rpc.order.ship.req.OperationLogReq;
import com.ctshk.rpc.order.ship.service.IOperationLogService;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author wansi <gaylen15527491065@yeah.net>
 * @Date 2021/5/26
 */
//@DubboService
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {

    @Autowired
    private OperationLogMapper operationLogMapper;
    @DubboReference
    private ISysUserService iSysUserService;

    @Override
    public PageResponse<OperationLogDTO> list(OperationLogReq operationLogReq) {
        IPage<OperationLog> operationLogIPage = null;
        // 分页条件查询
        QueryWrapper<OperationLog> operationLogQueryWrapper = new QueryWrapper<>();
        operationLogQueryWrapper.eq("model","SHIP_ORDER");
        operationLogQueryWrapper.ge(StringUtils.isNotBlank(operationLogReq.getGmtCreateStart()),"gmt_create",operationLogReq.getGmtCreateStart());
        operationLogQueryWrapper.le(StringUtils.isNotBlank(operationLogReq.getGmtCreateEnd()),"gmt_create",operationLogReq.getGmtCreateEnd());
        operationLogQueryWrapper.eq(StringUtils.isNotBlank(operationLogReq.getUserId()),"user_id",operationLogReq.getUserId());
        Page<OperationLog> iPage = new Page<>(operationLogReq.getPageNo(), operationLogReq.getPageSize());
        operationLogIPage = operationLogMapper.selectPage(iPage, operationLogQueryWrapper);
        if (CollectionUtils.isEmpty(operationLogIPage.getRecords())) {
            return PageResponse.empty(operationLogIPage.getCurrent());
        }
        List<OperationLogDTO> operationLogDTOS = new ArrayList<>();
        operationLogIPage.getRecords().forEach(operationLog -> {
            OperationLogDTO operationLogDTO = EntityUtil.copy(operationLog, OperationLogDTO.class);
            if(operationLog != null){
                //todo 上线去掉
                UserDTO userDTO = null;
                try {
                    userDTO = iSysUserService.loadUserByParam(operationLog.getUserId(), 1);
                }catch (Exception e){
                    throw new BusinessException(SystemError.SYS_502);
                }
                if(userDTO != null){
                    operationLogDTO.setCreateByName(userDTO.getUsername()+"/"+userDTO.getDept());
                }
            }
            operationLogDTOS.add(operationLogDTO);
        });
        PageResponse<OperationLogDTO> result = new PageResponse<>(operationLogDTOS, operationLogIPage.getCurrent() == 1, operationLogIPage.getTotal() <= (operationLogIPage.getCurrent() * operationLogIPage.getSize()), operationLogIPage.getTotal(), operationLogIPage.getCurrent(), operationLogIPage.getSize());
        return result;
    }
}
